package com.macro.mall.tiny.modules.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.macro.mall.tiny.modules.pms.dto.PmsRepairDataNode;
import com.macro.mall.tiny.modules.pms.mapper.PmsRepairDataMapper;
import com.macro.mall.tiny.modules.pms.model.PmsRepairData;
import com.macro.mall.tiny.modules.pms.service.PmsRepairDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 维修数据服务实现类
 * </p>
 *
 * @author tyrone
 * @since 2025-02-20
 */
@Service
public class PmsRepairDataServiceImpl extends ServiceImpl<PmsRepairDataMapper, PmsRepairData> implements PmsRepairDataService {

    @Override
    public List<PmsRepairDataNode> treeList(Integer type) {
        // 获取所有数据
        QueryWrapper<PmsRepairData> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort").orderByDesc("create_time");
        if (type != null) {
            wrapper.eq("type", type);
        }
        List<PmsRepairData> list = list(wrapper);
        
        // 转换成Node对象
        List<PmsRepairDataNode> nodeList = list.stream().map(item -> {
            PmsRepairDataNode node = new PmsRepairDataNode();
            BeanUtils.copyProperties(item, node);
            return node;
        }).collect(Collectors.toList());
        
        // 构建树形结构
        List<PmsRepairDataNode> result = new ArrayList<>();
        for (PmsRepairDataNode node : nodeList) {
            if (node.getParentId() == 0) { // 找到根节点
                node.setChildren(getChildren(node, nodeList));
                result.add(node);
            }
        }
        return result;
    }

    /**
     * 递归查找子节点
     */
    private List<PmsRepairDataNode> getChildren(PmsRepairDataNode parent, List<PmsRepairDataNode> nodeList) {
        List<PmsRepairDataNode> children = new ArrayList<>();
        for (PmsRepairDataNode node : nodeList) {
            if (node.getParentId().equals(parent.getId())) {
                node.setChildren(getChildren(node, nodeList));
                children.add(node);
            }
        }
        return children;
    }

    @Override
    @Transactional
    public boolean deleteWithChildren(Long id) {
        // 获取所有子节点ID
        List<Long> ids = new ArrayList<>();
        ids.add(id);
        collectChildIds(id, ids);
        
        // 批量删除
        return removeByIds(ids);
    }

    /**
     * 递归收集子节点ID
     */
    private void collectChildIds(Long parentId, List<Long> ids) {
        QueryWrapper<PmsRepairData> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId);
        List<PmsRepairData> children = list(wrapper);
        for (PmsRepairData child : children) {
            ids.add(child.getId());
            collectChildIds(child.getId(), ids);
        }
    }

    @Override
    public boolean updateShowStatus(Long id, Integer showStatus) {
        UpdateWrapper<PmsRepairData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                .set("show_status", showStatus);
        return update(updateWrapper);
    }

    @Override
    public boolean updateSort(Long id, Integer sort) {
        UpdateWrapper<PmsRepairData> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id)
                .set("sort", sort);
        return update(updateWrapper);
    }

    @Override
    public List<PmsRepairData> listByParentId(Long parentId) {
        QueryWrapper<PmsRepairData> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id", parentId)
              .orderByAsc("sort")
              .orderByDesc("create_time");
        return list(wrapper);
    }
}
